package main

import (
	"fmt"
	"gitee.com/amoyx/TailfLog"
	"log"
	"os"
	"time"
)

const (
	DebugLog  = "logs/debug.log"
	ErrorLog  = "logs/error.log"
	AccessLog = "logs/access.log"
	ServePort = 9999
)

func main() {
	// 测试
	tailflog.See("ErrorLog", ErrorLog)
	tailflog.See("DebugLog", DebugLog)
	tailflog.See("AccessLog", AccessLog)
	tailflog.Serve(ServePort, "seelog")
	// 模拟服务输出日志
	go printLog("ErrorLog", ErrorLog)
	go printLog("AccessLog", AccessLog)
	select {}
}

func printLog(name, path string) {
	// 模拟日志输出
	err := os.Remove(path)
	if err != nil {
		log.Println(err)
	}

	f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0777)
	if err != nil {
		log.Println(err)
		return
	}

	for t := range time.Tick(time.Second * 1) {
		testLog := fmt.Sprintf("「%s」[%s]\n", name, t.String())
		_, err = f.WriteString(testLog)
		if err != nil {
			log.Println(err.Error())
		}
	}
}
